<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Actions\Message;

use PsiFramework\Classes\Util\UI\Printer;

use PsiFramework\Classes\Factory\ObjectFactory;

use PsiFramework\Classes\Util\Object\Date;

use PsiFramework\Classes\Util\Object\EMail;

use PsiFramework\Applications\DefaultApp\Classes\Model\Message;

use PsiFramework\Classes\Util\Manager\ObjectManager;

use PsiFramework\Classes\Factory\DAOFactory;

use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de message
 *
 * @package Actions.Message
 */
class SendMessage extends MessageAction
{
    protected /*Message*/ $_message;
    
    /**
     * 
     * @return bool
     */
    public function action($recipient=null)
    {
        $ok = parent::action($recipient);
        
        if ($ok) {
            $title = $this->_request->getParameter('message_title');
            $content = $this->_request->getParameter('message_content');
            $conversationId = $this->_request->getParameter('conversation_id');
            $senderName = $this->_request->getParameter('sender_name');
            $senderIp = $this->_request->getParameter('sender_ip');
            
            if (isNullOrEmptyString($conversationId)) {
                $conversationId = uniqid();
            }   
                 
            $recipientEmail = $this->_request->getParameter('recipient_email');
            if (isNullOrEmptyString($recipientEmail) && is_null($recipient)) {
                $recipientId = $this->_request->getParameter('recipient_id');
                if (!isNullOrEmptyString($recipientId)) {
                    $userDao = DAOFactory::getDAO('User');
                    $recipient = $userDao->retrieveByPk($recipientId);
                }
            }
            
            if (isNullOrEmptyString($senderIp)) {
                $this->_response->setAttribute('message', 'NO_IP');
                return false;
            }
            if (isNullOrEmptyString($title)) {
                $this->_response->setAttribute('message', 'NO_TITLE');
                return false;
            }
            if (isNullOrEmptyString($content)) {
                $this->_response->setAttribute('message', 'NO_CONTENT');
                return false;
            }
            if (isNullOrEmptyString($recipientEmail) && is_null($recipient)) {
                $this->_response->setAttribute('message', 'NO_RECIPIENT');
                return false;
            }
                
            $senderEmail = $this->_request->getParameter('sender_email');
            
            $this->_message = ObjectFactory::getNewObject('Message');
            $this->_message->setConversationId($conversationId);
            $this->_message->setTitle($title);
            $this->_message->setContent($content);
            $this->_message->setDate(Date::now());
            $this->_message->setSenderIp($senderIp);
            $this->_message->setSenderName($senderName);
            
            $from = 'Unknown <noreply@noreply.com>';
            if (!isNullOrEmptyString($senderEmail)) {
                $this->_message->setSenderEmail($senderEmail);
                if (!isNullOrEmptyString($senderName)) {
                    $from = $senderName.' <noreply@noreply.com>';
                }
            } else if (!is_null($this->_user)) {
                $this->_message->setSender($this->_user);
                $from = $this->_user->getEmail();
            } else {
                $this->_response->setAttribute('message', 'NO_SENDER');
                return false;
            }
            
            $to = null;
            if (!is_null($recipient)) {
                $this->_message->setRecipient($recipient);
                $to = $recipient->getEmail();
            } else if (!isNullOrEmptyString($recipientEmail)) {
                $this->_message->setRecipientEmail($recipientEmail);
                $to = $recipientEmail;
            } else {
                $this->_response->setAttribute('message', 'NO_RECIPIENT');
                return false;
            }
            
            $messageDAO = DAOFactory::getDAO('Message');
            $ok = $messageDAO->save($this->_message);
            
            if (!$ok) {
                $this->_response->setAttribute('message', 'ERROR_DURING_SAVING');
                return false;
            }
            
            $emailContent = Printer::getText('Vous avez reçu un nouveau message par le biais de notre site :'."\n\n".$content."\n--\n".'L\'équipe Allo-Média reste à votre disposition.'."\n");
            
            $email = new EMail($from, $to, '[www.allo-media.pro] '.$title, $emailContent);
            $email->send();
        }
        
        return $ok;
    }
    
    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     */
    public function execute()
    {
        parent::execute();

        if ($this->action()) {
            $this->findForward("success");
        } else {
            $this->_response->setAttribute('recipientEmail', $this->_request->getParameter('recipient_email'));
            $this->_response->setAttribute('recipientId', $this->_request->getParameter('recipient_id'));
            $this->findForward("form");
        }
        
    }
}
